package com.example.shixun.controller;

import com.example.shixun.pojo.Course;
import com.example.shixun.pojo.CourseChapterLesson;
import com.example.shixun.pojo.Result;
import com.example.shixun.service.CourseChapterLessonService;
import com.example.shixun.service.CourseService;
import com.example.shixun.utils.GetNowTime;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/lesson")
public class LessonController {
    @Autowired
    CourseChapterLessonService lessonService;
    @Autowired
    CourseService courseService;
    @ApiOperation("添加课时")
    @PostMapping("/save")
    public Result saveLesson(@RequestBody CourseChapterLesson lesson){
        System.out.println("lesson:"+lesson.toString());
        if(lesson.getCreateTime()==null) lesson.setCreateTime(GetNowTime.getNowTime());
        if(lesson.getStartTime()==null) lesson.setStartTime(GetNowTime.getNowTime());
        if(lesson.getUpdateTime()==null) lesson.setUpdateTime(GetNowTime.getNowTime());
        Integer n = lessonService.saveCourseChapterLesson(lesson);
        if(n>0){
            Course course=new Course();
            course.setId(lesson.getCourseId());
            course.setUpdateTime(lesson.getUpdateTime());
            courseService.updateCourse(course);
            return Result.success("添加成功",lesson.getUpdateTime());
        }
        return Result.fail("添加失败");
    }
    @ApiOperation("根据课程id显示所有课时")
    @GetMapping("/findByCourseId")
    public Result findByCourseId(@RequestParam("id") Integer id){
        return Result.success(lessonService.findByCourseId(id));
    }
    @ApiOperation("删除课时")
    @GetMapping("/delete")
    public Result deleteLesson(@RequestParam("id") Integer id){
        return Result.success(lessonService.deleteCourseChapterLessonById(id));
    }
    @ApiOperation("根据id查找课时")
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")Integer id){
        return Result.success(lessonService.selectCourseChapterLessonById(id));
    }
}
